/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc;

import com.cxhd.dc.tool.FileUtil;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.io.File;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;

/**
 *
 * @author Administrator
 */
public class DataConfig {

    private static DataConfig dataConfig;

    public static final String version = "1.1";//版本号

    private String centerHost;//中心服务器地址

    private String checkHost;//检查是否有需要下载的

    private String port;//端口

    private String username;//用户名

    private String password;// 密码

    private String SN;// 中心的SN

    private String AET;//服务器AET

    private String ae_ip;//服务器Ip

    private int ae_port;//服务器端口

    private String otherFolder;//扫描指定的文件夹

    private List<String> reportList;//获取返回报告的对象

    private List<Map<String, String>> sendPacsParamList;//-修改不同医院对应着dicom信息

    private String dicomFolder;//存放dicom的文件夹

    private String basePath;//跟目录

    private boolean autoTask = false; //是否自动启动任务

    private String zipFolder = "zipFolder/";//存放zip 的文件夹

    private String tempUnzipFolder = "tempUnZipFolder/";//临时文件夹

    private String downloadFolder = "downloadFolder/";//下载的文件夹

    private String dicomSendFolder = "dicomSendFolder/";//解压之后dicom存放的文件夹

    private String otherFileFolder = "otherFileFolder/";//其他解压之后的文件格式存放的文件夹

    private String reportFolder = "k:/reports/remote/";//返回报告的文件夹

    private String scanReportFolder;//扫描报告的文件夹

    private String recviceXmlFolder = "recviceXmlFolder/";//存放解压的xml

    private String recviceSqlFolder = "recviceSqlFolder/";//存放解压的sql

    private String logSystemFolder = "logSystemFolder/";//存放系统运行日志

    private String logStudyFolder = "logStudyFolder/";//存放Study日志

    private String wliFolder = "wliStudyFolder/";//存放wli后缀的文件     

    private String warFolder = "warFolder/";//存放要更新的war 文件

    private String batFolder = "batFolder/";//存放要执行的BAT

    private String moveFileFolder = "moveFileFolder/";//存放要执行的BAT

    private String messageServer;//发送消息提醒的的服务器

    private boolean sendMessageFlag;// 是否发送消息

    private String groupId;//发送消息提醒的群ID

    private String loginPW = "efotoadmin";//配置界面验证密码

    private String localhostSN = System.getenv("COMPUTERNAME");

    private boolean returnReportFlag = true;

    private int upzipThread = 1;//压缩线程数

    private int unzipThread = 4;//解压线程数
    
    private int sendPacsThread = 1;//发送PACS线程数

    private String atlasServer;//服务器类型

    private String atlasFileServer;//云图文件系统

    private String report_sn;//返回报告的列表

    private String moveFile_sn;//移动文件到其他的文件夹的

    private String cacheServer;//发送DICOM到缓存的地址 aet@127.0.0.1:11118

    private String updateUrl = "www.tronsystem.com"; //更新地址

    private String projectName = "DataConfig.war";
    
    private String startWeb ="1"; //是否启动websocket 默认启动
    private String gcache = "0";//0/1缓存到Gcace中  默认不缓存
    
    private String recFileMode="0";//0默认转发到PACS 1：转成到gCache 2:两个同时进行
    
    private String patientIdEdit;//收到图之后是否修改病人Id
    
    private String noticeNo;//短信通知号码
    
    private String rptPath;// 二级中心转存路径

    public static DataConfig getInstance() {
        if (dataConfig == null) {

            dataConfig = new DataConfig();
        }
        return dataConfig;
    }

    public String getCenterHost() {
        return centerHost;
    }

    public void setCenterHost(String centerHost) {
        this.centerHost = centerHost;
    }

    public String getCheckHost() {
        return checkHost;
    }

    public void setCheckHost(String checkHost) {
        this.checkHost = checkHost;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAET() {
        return AET;
    }

    public void setAET(String AET) {
        this.AET = AET;
    }

    public String getAe_ip() {
        return ae_ip;
    }

    public void setAe_ip(String ae_ip) {
        this.ae_ip = ae_ip;
    }

    public int getAe_port() {
        return ae_port;
    }

    public void setAe_port(int ae_port) {
        this.ae_port = ae_port;
    }

    public String getOtherFolder() {
        return otherFolder;
    }

    public void setOtherFolder(String otherFolder) {
        this.otherFolder = otherFolder;
    }

    public List<String> getReportList() {
        return reportList;
    }

    public void setReportList(List<String> reportList) {
        this.reportList = reportList;
    }

    public List<Map<String, String>> getSendPacsParamList() {
        return sendPacsParamList;
    }

    public void setSendPacsParamList(List<Map<String, String>> sendPacsParamList) {
        this.sendPacsParamList = sendPacsParamList;
    }

    public String getDicomFolder() {
        return dicomFolder;
    }

    public void setDicomFolder(String dicomFolder) {
        this.dicomFolder = dicomFolder;
    }

    public String getZipFolder() {
        return zipFolder;
    }

    public void setZipFolder(String zipFolder) {
        this.zipFolder = zipFolder;
    }

    public String getScanReportFolder() {
        return scanReportFolder;
    }

    public void setScanReportFolder(String scanReportFolder) {
        this.scanReportFolder = scanReportFolder;
    }

    public String getSN() {
        return SN;
    }

    public void setSN(String SN) {
        this.SN = SN;
    }

    public DataConfig getConfigForDatabase() {
        List<Record> list = Db.use("dcmris_en").find("select * from dataconfig where other1='" + DataConfig.getInstance().getLocalhostSN() + "'");

        for (Record re : list) {

            if (re.getStr("name").equalsIgnoreCase("centerHost")) {
                setCenterHost(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("checkHost")) {
                setCheckHost(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("port")) {
                setPort(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("username")) {
                setUsername(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("password")) {
                setPassword(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("SN")) {
                setSN(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("AET")) {
                setAET(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("ae_ip")) {
                setAe_ip(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("ae_port")) {
                setAe_port(Integer.parseInt(re.getStr("value")));
            } else if (re.getStr("name").equalsIgnoreCase("dicomFolder")) {
                setDicomFolder(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("basePath")) {
                setBasePath(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("scanReportFolder")) {
                setScanReportFolder(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("autoTask")) {
                if (re.getStr("value") != null) {
                    setAutoTask(re.getStr("value").equals("true"));
                }

            } else if (re.getStr("name").equalsIgnoreCase("returnReportFlag")) {
                if (re.getStr("value") != null) {
                    setReturnReportFlag(re.getStr("value").equals("true"));
                }
            } else if (re.getStr("name").equalsIgnoreCase("groupId")) {
                setGroupId(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("messageServer")) {
                setMessageServer(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("sendMessageFlag")) {
                if (re.getStr("value") != null) {
                    setSendMessageFlag(re.getStr("value").equals("true"));
                }

            } else if (re.getStr("name").equalsIgnoreCase("atlasServer")) {
                setAtlasServer(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("atlasFileServer")) {
                setAtlasFileServer(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("report_sn")) {
                setReport_sn(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("moveFile_sn")) {
                setMoveFile_sn(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("cacheServer")) {
                setCacheServer(re.getStr("value"));
            }
            else if (re.getStr("name").equalsIgnoreCase("gcache")) {
                setGcache(re.getStr("value"));
            }
            
             else if (re.getStr("name").equalsIgnoreCase("sendPacsThread")) {
                setSendPacsThread(re.getStr("vaule")==null?1:Integer.parseInt(re.getStr("vaule")));
            }
            else if (re.getStr("name").equalsIgnoreCase("startWeb")) {
                setStartWeb(re.getStr("value"));
            }
             else if (re.getStr("name").equalsIgnoreCase("recFileMode")) {
                setRecFileMode(re.getStr("value"));
            }
             else if (re.getStr("name").equalsIgnoreCase("patientIdEdit")) {
                setPatientIdEdit(re.getStr("value"));
            }
             else if (re.getStr("name").equalsIgnoreCase("noticeNo")) {
                setNoticeNo(re.getStr("value"));
            } else if (re.getStr("name").equalsIgnoreCase("rptPath")) {
                setRptPath(re.getStr("value"));
            }
        }
        System.out.println(getCacheServer());
        return this;
    }

    public DataConfig checkFolder() {
        File f = new File(getBasePath() + "/" + getZipFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getTempUnzipFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getDownloadFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getOtherFileFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getDicomSendFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getRecviceSqlFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getRecviceXmlFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getLogSystemFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getLogStudyFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getWliFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getWarFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getBatFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(getBasePath() + "/" + getMoveFileFolder());
        if (!f.exists()) {
            f.mkdirs();
        }
        return this;

    }

    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }

    public String getTempUnzipFolder() {
        return tempUnzipFolder;
    }

    public void setTempUnzipFolder(String tempUnzipFolder) {
        this.tempUnzipFolder = tempUnzipFolder;
    }

    public String getLoginPW() {
        return loginPW;
    }

    public void setLoginPW(String loginPW) {
        this.loginPW = loginPW;
    }

    public String getLocalhostSN() {
        return localhostSN;
    }

    public void setLocalhostSN(String localhostSN) {
        this.localhostSN = localhostSN;
    }

    public String getDownloadFolder() {
        return downloadFolder;
    }

    public void setDownloadFolder(String downloadFolder) {
        this.downloadFolder = downloadFolder;
    }

    public String getDicomSendFolder() {
        return dicomSendFolder;
    }

    public void setDicomSendFolder(String dicomSendFolder) {
        this.dicomSendFolder = dicomSendFolder;
    }

    public String getOtherFileFolder() {
        return otherFileFolder;
    }

    public void setOtherFileFolder(String otherFileFolder) {
        this.otherFileFolder = otherFileFolder;
    }

    public int getUpzipThread() {
        return upzipThread;
    }

    public void setUpzipThread(int upzipThread) {
        this.upzipThread = upzipThread;
    }

    public int getUnzipThread() {
        return unzipThread;
    }

    public void setUnzipThread(int unzipThread) {
        this.unzipThread = unzipThread;
    }

    public boolean isReturnReportFlag() {
        return returnReportFlag;
    }

    public void setReturnReportFlag(boolean returnReportFlag) {
        this.returnReportFlag = returnReportFlag;
    }

    public String getReportFolder() {
        return reportFolder;
    }

    public void setReportFolder(String reportFolder) {
        this.reportFolder = reportFolder;
    }

    public boolean isAutoTask() {
        return autoTask;
    }

    public void setAutoTask(boolean autoTask) {
        this.autoTask = autoTask;
    }

    public String getRecviceXmlFolder() {
        return recviceXmlFolder;
    }

    public void setRecviceXmlFolder(String recviceXmlFolder) {
        this.recviceXmlFolder = recviceXmlFolder;
    }

    public String getRecviceSqlFolder() {
        return recviceSqlFolder;
    }

    public void setRecviceSqlFolder(String recviceSqlFolder) {
        this.recviceSqlFolder = recviceSqlFolder;
    }

    public String getLogSystemFolder() {
        return logSystemFolder;
    }

    public void setLogSystemFolder(String logSystemFolder) {
        this.logSystemFolder = logSystemFolder;
    }

    public String getLogStudyFolder() {
        return logStudyFolder;
    }

    public void setLogStudyFolder(String logStudyFolder) {
        this.logStudyFolder = logStudyFolder;
    }

    public String getMessageServer() {
        return messageServer;
    }

    public void setMessageServer(String messageServer) {
        this.messageServer = messageServer;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public boolean isSendMessageFlag() {
        return sendMessageFlag;
    }

    public void setSendMessageFlag(boolean sendMessageFlag) {
        this.sendMessageFlag = sendMessageFlag;
    }

    public String getAtlasFileServer() {
        return atlasFileServer;
    }

    public void setAtlasFileServer(String atlasFileServer) {
        this.atlasFileServer = atlasFileServer;
    }

    public String getAtlasServer() {
        return atlasServer;
    }

    public void setAtlasServer(String atlasServer) {
        this.atlasServer = atlasServer;
    }

    public String getReport_sn() {
        return report_sn;
    }

    public void setReport_sn(String report_sn) {
        this.report_sn = report_sn;
    }

    public String getWliFolder() {
        return wliFolder;
    }

    public void setWliFolder(String wliFolder) {
        this.wliFolder = wliFolder;
    }

    public String getCacheServer() {
        return cacheServer;
    }

    public void setCacheServer(String cacheServer) {
        this.cacheServer = cacheServer;
    }

    public String getWarFolder() {
        return warFolder;
    }

    public void setWarFolder(String warFolder) {
        this.warFolder = warFolder;
    }

    public String getBatFolder() {
        return batFolder;
    }

    public void setBatFolder(String batFolder) {
        this.batFolder = batFolder;
    }

    public String getUpdateUrl() {
        return updateUrl;
    }

    public void setUpdateUrl(String updateUrl) {
        this.updateUrl = updateUrl;
    }

    public String getTomcatRoot() {

        ServletContext servletContext = JFinal.me().getServletContext();
        String tomcatRoot = servletContext.getRealPath("/").replace("\\", "/");
        tomcatRoot = tomcatRoot.substring(0, tomcatRoot.lastIndexOf("/"));
        tomcatRoot = tomcatRoot.substring(0, tomcatRoot.lastIndexOf("/"));
        tomcatRoot = tomcatRoot.substring(0, tomcatRoot.lastIndexOf("/"));
        tomcatRoot = tomcatRoot.substring(0, tomcatRoot.lastIndexOf("/"));

        return tomcatRoot;
    }

    public void CreateBat() {

        StringBuilder sb = new StringBuilder();
        sb.append("timeout 10").append(System.getProperty("line.separator")).append("del ").append(getTomcatRoot()).append("/").append(projectName)
                .append(System.getProperty("line.separator"))
                .append("timeout 20")
                .append(System.getProperty("line.separator")).append("move ").append(getBasePath()).append("/").append(getWarFolder()).append(projectName).append(" ").append(getTomcatRoot())
                .append("/")
                .append(projectName)
                .append(System.getProperty("line.separator"));
        FileUtil.writeText(getBasePath() + "/" + getBatFolder() + projectName + ".bat", sb.toString());

    }

    public String getMoveFileFolder() {
        return moveFileFolder;
    }

    public void setMoveFileFolder(String moveFileFolder) {
        this.moveFileFolder = moveFileFolder;
    }

    public String getMoveFile_sn() {
        return moveFile_sn;
    }

    public void setMoveFile_sn(String moveFile_sn) {
        this.moveFile_sn = moveFile_sn;
    }

    public String getGcache() {
        return gcache;
    }

    public void setGcache(String gcache) {
        this.gcache = gcache;
    }

    public int getSendPacsThread() {
        return sendPacsThread;
    }

    public void setSendPacsThread(int sendPacsThread) {
        this.sendPacsThread = sendPacsThread;
    }

    public String getStartWeb() {
        return startWeb;
    }

    public void setStartWeb(String startWeb) {
        this.startWeb = startWeb;
    }

    public String getRecFileMode() {
        return recFileMode;
    }

    public void setRecFileMode(String recFileMode) {
        this.recFileMode = recFileMode;
    }

    public String getPatientIdEdit() {
        return patientIdEdit;
    }

    public void setPatientIdEdit(String patientIdEdit) {
        this.patientIdEdit = patientIdEdit;
    }

    public String getNoticeNo() {
        return noticeNo;
    }

    public void setNoticeNo(String noticeNo) {
        this.noticeNo = noticeNo;
    }

    public String getRptPath() {
        return rptPath;
    }

    public void setRptPath(String rptPath) {
        this.rptPath = rptPath;
    }
}
